package com.shiyi.shiyiaiagent.chatMemory.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.ai.chat.messages.*;
import org.springframework.messaging.support.GenericMessage;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Map;

@Data
@TableName("chat_messages")
public class ChatMessage {
    @TableId(type = IdType.AUTO)
    private Long id;

    @TableField("conversation_id")
    private String conversationId;

    private String role;

    private String content;

    @TableField("created_at")
    private LocalDateTime createdAt;

    public static ChatMessage fromMessage(String conversationId, Message message) {
        ChatMessage entity = new ChatMessage();
        entity.setConversationId(conversationId);
        entity.setRole(message.getMessageType().name());
        entity.setContent(message.getText());
        return entity;
    }


  public Message toMessage() {
    MessageType type = MessageType.valueOf(role);
    return switch (type) {
      case ASSISTANT -> new AssistantMessage(content);
      case USER -> new UserMessage(content);
      case SYSTEM -> new SystemMessage(content);
      case TOOL -> null;
    };
  }

//  public Message toMessage() {
//    return new Message() {
//      @Override
//      public String getText() {
//        return content;
//      }
//
//      @Override
//      public MessageType getMessageType() {
//        return role != null ? MessageType.valueOf(role) : MessageType.ASSISTANT;
//      }
//
//      @Override
//      public Map<String, Object> getMetadata() {
//        return Collections.emptyMap();
//      }
//    };
//  }
}
